using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
using WPFExcelReport.Helpers;
using WPFExcelReport.Controls;
namespace WPFExcelReport
{
    public partial class VPTBILD
    {
        public DataRow Convert2PTBILD(DataRow dwInput, string ITXDBVERSION, bool IsConvert = false)
        {
            DataSet SResult = new DataSet();
            SResult = QueryPTBILD(dwInput, 0);
            DataRow dwReturn = null;
            if (SResult.Tables[0].Rows.Count > 0)
            {
                dwReturn = SResult.Tables[0].Rows[0];
            }
            else
            {
                dwReturn = SResult.Tables[0].NewRow();
            }
            switch (ITXDBVERSION)
            {
                case "YGR":
                    #region=========
         dwReturn["PTBMANDANT"] = " ".PadRight(2 ); 
         dwReturn["PTBPAUFKRS"] = " ".PadRight(2 ); 
         dwReturn["PTBPAUFLFD"] = " ".PadRight(6 ); 
         dwReturn["PTBLFDNR"] = " ".PadRight(3 ); 
         dwReturn["PTBAENZL"] = " ".PadRight(0 ); 
         dwReturn["PTBAUFKRS"] = " ".PadRight(2 ); 
         dwReturn["PTBAUFLFD"] = " ".PadRight(6 ); 
         dwReturn["PTBAUFPOS"] = " ".PadRight(3 ); 
         dwReturn["PTBAFOLGE"] = " ".PadRight(3 ); 
         dwReturn["PTBTP"] = " ".PadRight(4 ); 
         dwReturn["PTBRWKRS"] = " ".PadRight(2 ); 
         dwReturn["PTBRWLFD"] = " ".PadRight(6 ); 
         dwReturn["PTBRWPOS"] = " ".PadRight(4 ); 
         dwReturn["PTBPAFOLGE"] = " ".PadRight(3 ); 
         dwReturn["PTBSME"] = " ".PadRight(1 ); 
         dwReturn["PTBSMG1"] = " ".PadRight(0 ); 
         dwReturn["PTBSMG2"] = " ".PadRight(0 ); 
         dwReturn["PTBSMG3"] = " ".PadRight(0 ); 
         dwReturn["PTBSMG4"] = " ".PadRight(0 ); 
         dwReturn["PTBSMG5"] = " ".PadRight(0 ); 
         dwReturn["PTBSMG6"] = " ".PadRight(0 ); 
         dwReturn["PTBSMG7"] = " ".PadRight(0 ); 
         dwReturn["PTBSMG8"] = " ".PadRight(0 ); 
         dwReturn["PTBFILLER"] = " ".PadRight(19 ); 
                    #endregion
                    break;
                case "JLT":

                    break;
                default:

                    break;
            }
            if (IsConvert)
            {
                foreach (DataColumn cm in dwInput.Table.Columns)
                {
                    if (dwInput[cm.ColumnName].ToString() == "")
                        continue;
                    if (dwReturn.Table.Columns.Contains(cm.ColumnName))
                    {
                        dwReturn[cm.ColumnName] = dwInput[cm.ColumnName];
                    }
                }
            }
            return dwReturn;
        }
        public void MapPTBILD(ref DataRow dwLocal, DataRow dwData)
        {
            BlindMap(ref dwLocal, dwData);
        }
        public void SavePTBILD(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            DataSet setResult = new DataSet();
            DataTable tbLocal = null;
            DataRow dwLocal = null;
            bool flgExist = false;
            setResult = QueryPTBILD(dwData);
            if (setResult.Tables.Count > 0)
            {
                tbLocal = setResult.Tables[0];
                if (tbLocal.Rows.Count > 0)
                {
                    flgExist = true;
                    dwLocal = tbLocal.Rows[0];
                }
                else
                {
                    dwLocal = tbLocal.NewRow();
                }
            }
            MapPTBILD(ref dwLocal, dwData);

            if (!flgExist)
            {

                InsertPTBILD(dwLocal, ITXDBVERSION);
            }
            UpdatePTBILD(dwLocal);
        }
        public void InsertPTBILD(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            switch (ITXDBVERSION)
            {
                case "YGR":
                    or1.CommandText = "INSERT INTO PTBILD(VAKBMANDANT, VAKBKOLL, VAKBART, VAKBKUNDNR, VAKBFARBE, VAKBKDARTNR, VAKBKDARTBEZ, VAKBKDFARBNR, VAKBANDRUCK, VAKBMEGEW, VAKBMENGEH, VAKBGEWVON, VAKBGEWBIS, VAKBGEWCA, VAKBGEWFKT, VAKBGEWGRP, VAKBMEGFKT, VAKBMEGGRP, VAKBAENDZL, VAKBAENDBEN, VAKBAENDDAT, VAKBNEUBEN, VAKBNEUDAT, VAKBFILLER)"
                        + " VALUES (:VAKBMANDANT, :VAKBKOLL, :VAKBART, :VAKBKUNDNR, :VAKBFARBE, '                         ', '                         ', '                             ', ' ', '   ', '   ', 0, 0, ' ', '000000000000', ' ', '000000000000', ' ', 0, '        ', '000000', '        ', '      ', '                                          ')";
                    or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                //VAFMANDANT VAFKOLL VAFART VAFFARBE VAFBEZ VAFFFARBE

            }
            
            Connect2Proxy(or1);
        }
        public DataSet QueryPTBILD(DataRow dwData, int option = 1)
        {
            DataSet Return;
            DataSet Result = new DataSet();
            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 0:
                    or1.CommandText = "SELECT * FROM PTBILD WHERE  ROWNUM<2 ";
                    break;
              
                case 99:
                    or1.CommandText = "SELECT * FROM PTBILD  WHERE ROWNUM<50";
                    break;
                default:
                    break;
            }
            Result = Connect2Proxy(or1);
            if (Result.Tables.Count > 0)
                Result.Tables[0].TableName = "PTBILD";
            Return = Result;
            return Return;
        }
        public void DeletePTBILD(DataRow dwData, int option = 1)
        {

            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    or1.CommandText = "DELETE FROM PTBILD   WHERE  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                     or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                default:
                    break;
            }
            Connect2Proxy(or1);
        } 
      
        public void UpdatePTBILD(DataRow dwData, string ITXDBVERSION = "50.24", int option = 1)
        {
            OracleCommand or1;
            DataSet Result = new DataSet();
            or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    #region====Command==== 
                    or1.CommandText = "UPDATE PTBILD SET "
                    +"PTBMANDANT= :PTBMANDANT,"
                    +"PTBPAUFKRS= :PTBPAUFKRS,"
                    +"PTBPAUFLFD= :PTBPAUFLFD,"
                    +"PTBLFDNR= :PTBLFDNR,"
                    +"PTBAENZL= :PTBAENZL,"
                    +"PTBAUFKRS= :PTBAUFKRS,"
                    +"PTBAUFLFD= :PTBAUFLFD,"
                    +"PTBAUFPOS= :PTBAUFPOS,"
                    +"PTBAFOLGE= :PTBAFOLGE,"
                    +"PTBTP= :PTBTP,"
                    +"PTBRWKRS= :PTBRWKRS,"
                    +"PTBRWLFD= :PTBRWLFD,"
                    +"PTBRWPOS= :PTBRWPOS,"
                    +"PTBPAFOLGE= :PTBPAFOLGE,"
                    +"PTBSME= :PTBSME,"
                    +"PTBSMG1= :PTBSMG1,"
                    +"PTBSMG2= :PTBSMG2,"
                    +"PTBSMG3= :PTBSMG3,"
                    +"PTBSMG4= :PTBSMG4,"
                    +"PTBSMG5= :PTBSMG5,"
                    +"PTBSMG6= :PTBSMG6,"
                    +"PTBSMG7= :PTBSMG7,"
                    +"PTBSMG8= :PTBSMG8,"
                    +"PTBFILLER= :PTBFILLER,"
                        + " WHERE  "
                        + "  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                    #endregion

                    #region====Parameters====
          or1.Parameters.Add("PTBMANDANT", OracleType.NVarChar).Value = dwData["PTBMANDANT"].ToString(); 
          or1.Parameters.Add("PTBPAUFKRS", OracleType.NVarChar).Value = dwData["PTBPAUFKRS"].ToString(); 
          or1.Parameters.Add("PTBPAUFLFD", OracleType.NVarChar).Value = dwData["PTBPAUFLFD"].ToString(); 
          or1.Parameters.Add("PTBLFDNR", OracleType.NVarChar).Value = dwData["PTBLFDNR"].ToString(); 
          or1.Parameters.Add("PTBAENZL", OracleType.NVarChar).Value = dwData["PTBAENZL"].ToString(); 
          or1.Parameters.Add("PTBAUFKRS", OracleType.NVarChar).Value = dwData["PTBAUFKRS"].ToString(); 
          or1.Parameters.Add("PTBAUFLFD", OracleType.NVarChar).Value = dwData["PTBAUFLFD"].ToString(); 
          or1.Parameters.Add("PTBAUFPOS", OracleType.NVarChar).Value = dwData["PTBAUFPOS"].ToString(); 
          or1.Parameters.Add("PTBAFOLGE", OracleType.NVarChar).Value = dwData["PTBAFOLGE"].ToString(); 
          or1.Parameters.Add("PTBTP", OracleType.NVarChar).Value = dwData["PTBTP"].ToString(); 
          or1.Parameters.Add("PTBRWKRS", OracleType.NVarChar).Value = dwData["PTBRWKRS"].ToString(); 
          or1.Parameters.Add("PTBRWLFD", OracleType.NVarChar).Value = dwData["PTBRWLFD"].ToString(); 
          or1.Parameters.Add("PTBRWPOS", OracleType.NVarChar).Value = dwData["PTBRWPOS"].ToString(); 
          or1.Parameters.Add("PTBPAFOLGE", OracleType.NVarChar).Value = dwData["PTBPAFOLGE"].ToString(); 
          or1.Parameters.Add("PTBSME", OracleType.NVarChar).Value = dwData["PTBSME"].ToString(); 
          or1.Parameters.Add("PTBSMG1", OracleType.NVarChar).Value = dwData["PTBSMG1"].ToString(); 
          or1.Parameters.Add("PTBSMG2", OracleType.NVarChar).Value = dwData["PTBSMG2"].ToString(); 
          or1.Parameters.Add("PTBSMG3", OracleType.NVarChar).Value = dwData["PTBSMG3"].ToString(); 
          or1.Parameters.Add("PTBSMG4", OracleType.NVarChar).Value = dwData["PTBSMG4"].ToString(); 
          or1.Parameters.Add("PTBSMG5", OracleType.NVarChar).Value = dwData["PTBSMG5"].ToString(); 
          or1.Parameters.Add("PTBSMG6", OracleType.NVarChar).Value = dwData["PTBSMG6"].ToString(); 
          or1.Parameters.Add("PTBSMG7", OracleType.NVarChar).Value = dwData["PTBSMG7"].ToString(); 
          or1.Parameters.Add("PTBSMG8", OracleType.NVarChar).Value = dwData["PTBSMG8"].ToString(); 
          or1.Parameters.Add("PTBFILLER", OracleType.NVarChar).Value = dwData["PTBFILLER"].ToString(); 
                    #endregion
                    break; 
                default:
                    break;
            }


            Connect2Proxy(or1);
        }
    }
}
